﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CardLogistics.DAL;
using CardLogistics.Web.Components;
using System.Data;
using System.Text;

namespace CardLogistics.Web.MainPage.Report.DepReport
{
    public partial class C_MonthRpt : BasePage
    {
        private static readonly DepReportData DepDAL = new DepReportData();
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_Search_Click(object sender, EventArgs e)
        {
            if (txt_Data.Text.Trim().Length > 0)
            {
                string startV = txt_Data.Text + "-01";//统计月份的第一天
                string endV = DateTime.Parse(startV).AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd");//统计月份的最后一天
                BindData(startV, endV);
            }
            else
            {
                ShowMessageBox("请选择统计月份！");
                txt_Data.Focus();
            }
        }
        /// <summary>
        /// 根据时间段查询报表数据
        /// </summary>
        /// <param name="startData"></param>
        /// <param name="endData"></param>
        private void BindData(string startData, string endData)
        {
            DataTable dt = DepDAL.GetJMonthInv(startData, endData);
            StringBuilder sbStr = new StringBuilder();//拼凑报表格式
            sbStr.Append("<div style='text-align: center; font-size: 25px; font-weight: bold'>西安城市一卡通有限责任公司卡片统计汇总月报表</div>");
            sbStr.Append("<table  style='border:0;width:100%;'><tr><td colspan='5' style='border:0;text-align:left;'>填报单位:</td><td style='border: 0; text-align: right;'>结束时间:" + startData + "至" + endData + "</td><td  colspan='" + (dt.Rows.Count * 3 - 4) + "' style='border:0;text-align:right;'>填表日期:" + DateTime.Now.ToString("yyyy-MM-dd") + "</td></tr></table>");
            sbStr.Append("<table  border='1' style='font-size:16px;white-space: nowrap; '>");

            StringBuilder sbThCard = new StringBuilder("<tr><td rowspan='2'colspan='2'></td>");//卡类型名称
            StringBuilder sbThNPT = new StringBuilder("<tr>");//数量、单据、总计
            StringBuilder sbThSQ_BaiKa = new StringBuilder("<tr><td colspan='2' style='width:180px;'>上期库存</td>");//上期结存(白卡、未售卡、制卡坏卡、未售废卡)
            //StringBuilder sbThSQ_WSK = new StringBuilder("<tr><td>未售卡</td>");//上期结存-未售卡
            //StringBuilder sbThSQ_ZKHK = new StringBuilder("<tr><td>制卡坏卡</td>");//上期结存-未售卡
            //StringBuilder sbThSQ_WSFK = new StringBuilder("<tr><td>未售废卡</td>");//上期结存-未售卡

            StringBuilder sbThBQ_BaiKa = new StringBuilder("<tr><td colspan='2'>新购入库白卡</td>");//本期结存-白卡
            //StringBuilder sbThBQ_ZKHK = new StringBuilder("<td colspan='2'>本期初制卡坏卡</td>");//本期结存-制卡坏卡
            StringBuilder sbTHBQ_FC = new StringBuilder("<tr><td colspan='2'>本期售出</td>");//本期结存-本期发出
            //StringBuilder sbTHBQ_WSHK = new StringBuilder("<td colspan='2'>本期收回未售坏卡</td>");//本期结存-本期收回未售坏卡
            //StringBuilder sbTHBQ_FHCJ = new StringBuilder("<td colspan='2'>本期返回厂家坏卡</td>");//本期结存-本期返回厂家坏卡

            StringBuilder sbThBKKC_BaiKa = new StringBuilder("<tr><td rowspan='2' style='width:90px;'>白卡库存</td><td style='width:90px;'>白卡</td>");//结存-白卡
            StringBuilder sbThBKKC_ZKHK = new StringBuilder("<tr><td>制卡坏卡</td>");//结存-白卡制卡坏卡

            StringBuilder sbThCPKC_WSK = new StringBuilder("<tr><td rowspan='2'>半/成品卡库存</td><td>成品卡</td>");//结存-成品卡库存
            StringBuilder sbThCPKC_WSFK = new StringBuilder("<tr><td>未售坏卡</td>");//结存-未售卡未售废卡(成品半成品未售坏卡库存)

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                sbThCard.Append("<td colspan='3'>" + dt.Rows[i]["cardtypename"].ToString() + "</td>");
                sbThNPT.Append("<td style='width:90px;'>数 量(张)</td><td style='width:90px;'>单 价(元)</td><td style='width:90px;'>金 额(元)</td>");
                sbThSQ_BaiKa.Append("<td>" + (int.Parse(dt.Rows[i]["上期白卡"].ToString()) + int.Parse(dt.Rows[i]["上期未售卡"].ToString()) + int.Parse(dt.Rows[i]["上期制卡坏卡"].ToString()) + int.Parse(dt.Rows[i]["上期未售废卡"].ToString())) + "</td><td></td><td>"
                    + (int.Parse(dt.Rows[i]["上期白卡金额"].ToString()) + int.Parse(dt.Rows[i]["上期未售卡金额"].ToString()) + int.Parse(dt.Rows[i]["上期制卡坏卡金额"].ToString()) + int.Parse(dt.Rows[i]["上期未售废卡金额"].ToString())) + "</td>");
                
                
                //sbThSQ_WSK.Append("<td>" + dt.Rows[i]["上期未售卡"].ToString() + "</td><td></td><td>" + dt.Rows[i]["上期未售卡金额"].ToString() + "</td>");
                //sbThSQ_ZKHK.Append("<td>" + dt.Rows[i]["上期制卡坏卡"].ToString() + "</td><td></td><td>" + dt.Rows[i]["上期制卡坏卡金额"].ToString() + "</td>");
                //sbThSQ_WSFK.Append("<td>" + dt.Rows[i]["上期未售废卡"].ToString() + "</td><td></td><td>" + dt.Rows[i]["上期未售废卡金额"].ToString() + "</td>");

                sbThBQ_BaiKa.Append("<td>" + dt.Rows[i]["本期入库"].ToString() + "</td><td></td><td>" + dt.Rows[i]["本期入库金额"].ToString() + "</td>");
                //sbThBQ_ZKHK.Append("<td>" + dt.Rows[i]["本期初制卡坏卡"].ToString() + "</td><td></td><td>" + dt.Rows[i]["本期初制卡坏卡金额"].ToString() + "</td>");
                sbTHBQ_FC.Append("<td>" + dt.Rows[i]["本期发出"].ToString() + "</td><td></td><td>" + dt.Rows[i]["本期发出金额"].ToString() + "</td>");
                //sbTHBQ_WSHK.Append("<td>" + dt.Rows[i]["本期收回未售坏卡"].ToString() + "</td><td></td><td>" + dt.Rows[i]["本期收回未售坏卡金额"].ToString() + "</td>");
                //sbTHBQ_FHCJ.Append("<td>" + dt.Rows[i]["本期返回厂家坏卡"].ToString() + "</td><td></td><td>" + dt.Rows[i]["本期返回厂家坏卡金额"].ToString() + "</td>");

                sbThBKKC_BaiKa.Append("<td>" + dt.Rows[i]["白卡库存"].ToString() + "</td><td></td><td>" + dt.Rows[i]["白卡库存金额"].ToString() + "</td>");
                sbThCPKC_WSK.Append("<td>" + dt.Rows[i]["未售卡库存"].ToString() + "</td><td></td><td>" + dt.Rows[i]["未售卡库存金额"].ToString() + "</td>");
                sbThBKKC_ZKHK.Append("<td>" + dt.Rows[i]["制卡坏卡库存"].ToString() + "</td><td></td><td>" + dt.Rows[i]["制卡坏卡库存金额"].ToString() + "</td>");
                sbThCPKC_WSFK.Append("<td>" + dt.Rows[i]["未售坏卡库存"].ToString() + "</td><td></td><td>" + dt.Rows[i]["未售坏卡库存金额"].ToString() + "</td>");

            }

            sbThCard.Append("<td colspan='2'>合计</td></tr>");
            sbThNPT.Append("<td style='width:90px;'>数 量(张)</td><td style='width:90px;'>金 额(元)</td></tr>");
            sbThSQ_BaiKa.Append("<td>" + dt.Compute("sum(上期白卡)", "true").ToString() + "</td><td>" + dt.Compute("sum(上期白卡金额)", "true").ToString() + "</td></tr>");
            //sbThSQ_WSK.Append("<td>" + dt.Compute("sum(上期未售卡)", "true").ToString() + "</td><td>" + dt.Compute("sum(上期未售卡金额)", "true").ToString() + "</td></tr>");
            //sbThSQ_ZKHK.Append("<td>" + dt.Compute("sum(上期制卡坏卡)", "true").ToString() + "</td><td>" + dt.Compute("sum(上期制卡坏卡金额)", "true").ToString() + "</td></tr>");
            //sbThSQ_WSFK.Append("<td>" + dt.Compute("sum(上期未售废卡)", "true").ToString() + "</td><td>" + dt.Compute("sum(上期未售废卡)", "true").ToString() + "</td></tr>");

            sbThBQ_BaiKa.Append("<td>" + dt.Compute("sum(本期入库)", "true").ToString() + "</td><td>" + dt.Compute("sum(本期入库金额)", "true").ToString() + "</td></tr>");
            //sbThBQ_ZKHK.Append("<td>" + dt.Compute("sum(本期初制卡坏卡)", "true").ToString() + "</td><td>" + dt.Compute("sum(本期初制卡坏卡金额)", "true").ToString() + "</td></tr>");
            sbTHBQ_FC.Append("<td>" + dt.Compute("sum(本期发出)", "true").ToString() + "</td><td>" + dt.Compute("sum(本期发出金额)", "true").ToString() + "</td></tr>");
            //sbTHBQ_WSHK.Append("<td>" + dt.Compute("sum(本期收回未售坏卡)", "true").ToString() + "</td><td>" + dt.Compute("sum(本期收回未售坏卡金额)", "true").ToString() + "</td></tr>");
            //sbTHBQ_FHCJ.Append("<td>" + dt.Compute("sum(本期返回厂家坏卡)", "true").ToString() + "</td><td>" + dt.Compute("sum(本期返回厂家坏卡金额)", "true").ToString() + "</td></tr>");

            sbThBKKC_BaiKa.Append("<td>" + dt.Compute("sum(白卡库存)", "true").ToString() + "</td><td>" + dt.Compute("sum(白卡库存金额)", "true").ToString() + "</td></tr>");
            sbThCPKC_WSK.Append("<td>" + dt.Compute("sum(未售卡库存)", "true").ToString() + "</td><td>" + dt.Compute("sum(未售卡库存金额)", "true").ToString() + "</td></tr>");
            sbThBKKC_ZKHK.Append("<td>" + dt.Compute("sum(制卡坏卡库存)", "true").ToString() + "</td><td>" + dt.Compute("sum(制卡坏卡库存金额)", "true").ToString() + "</td></tr>");
            sbThCPKC_WSFK.Append("<td>" + dt.Compute("sum(未售坏卡库存)", "true").ToString() + "</td><td>" + dt.Compute("sum(未售坏卡库存金额)", "true").ToString() + "</td></tr>");


            //sbStr.Append(sbThCard).Append(sbThNPT).Append(sbThSQ_BaiKa).Append(sbThSQ_WSK).Append(sbThSQ_ZKHK).Append(sbThSQ_WSFK);
            //sbStr.Append(sbThBQ_BaiKa).Append(sbThBQ_ZKHK).Append(sbTHBQ_FC).Append(sbTHBQ_WSHK).Append(sbTHBQ_FHCJ);
            //sbStr.Append(sbThBKKC_BaiKa).Append(sbThCPKC_WSK).Append(sbThBKKC_ZKHK).Append(sbThCPKC_WSFK);
            sbStr.Append(sbThCard).Append(sbThNPT).Append(sbThSQ_BaiKa);
            sbStr.Append(sbThBQ_BaiKa).Append(sbTHBQ_FC);
            sbStr.Append(sbThBKKC_BaiKa).Append(sbThBKKC_ZKHK).Append(sbThCPKC_WSK).Append(sbThCPKC_WSFK);
            d_info.InnerHtml = sbStr.ToString() + "</table>";
        }
        /// <summary>
        /// 导出报表内容
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnExport_Click(object sender, EventArgs e)
        {
            if (d_info.InnerHtml.Length > 50)
            {
                ExportExcel(txt_Data.Text, d_info);
            }
            else
            {
                ShowMessageBox("请先查询出报表内容！");
            }
        }
    }
}